<?php

namespace app\member\controller;

use app\member\middleware\Check;
use app\member\service\MsgService;

class Msg
{
    protected $middleware = [
        Check::class
    ];
    /**********************************************我的消息*****************************************************/

    public function myMsg(){
        return view("myMsg");
    }

    public function listMyMsg(){
        try {
            $uid = session("adid");
            $where = [];
            array_push($where,["receiver",'=',$uid]);
            $page = input("page");
            $limit = input("limit");
            $service = new MsgService();
            $res = $service->list($page,$limit,$where);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function doRead(){
        try{
            $id = request()->post("id");
            $data = [
                "id"=>$id,
                "is_read"=>1
            ];
            $service = new MsgService();
            $res = $service->edit($data);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }



    /**********************************************我的消息*****************************************************/

    /**********************************************消息列表*****************************************************/

    public function msg(){
        return view();
    }

    public function listMsg(){
        try{
            $service = new MsgService();
            $page = request()->get('page');
            $limit = request()->get('limit');
            $res = $service->list($page, $limit);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function addMsg(){
        try{
            $data = request()->post();
            $service = new MsgService();
            $res = $service->add($data);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function editMsg(){
        try{
            $data = request()->post();
            $service = new MsgService();
            $res = $service->edit($data);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }

    public function softdeleteMsg(){
        try{
            $ids = request()->post('ids');
            $service = new MsgService();
            $res = $service->softdelete($ids);
            return json($res);
        }catch (\Throwable $ex){
            return json(AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage()));
        }
    }


    /**********************************************消息列表*****************************************************/



}